package com.medicine.manage.block.service;

import com.medicine.manage.block.WeBASEUtils;
import com.medicine.manage.block.entity.WeBaseResult;
import com.medicine.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public abstract class AbstractBlockMultiplexService<T>  extends AbstractBlockService<T> implements IBlockMultiplexService<T>{

    @Autowired
    private IUserExtendService userExtendService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private WeBASEUtils weBASEUtils;



    @Override
    public WeBaseResult insertList(List<T> objList, Long deptId, Long optionUserId, String functionName) {

        // 查询该煎药机构的合约address
        String contractAddress = deptService.getContractAddress(deptId);
        // 查询该操作员的address
        String userName = userExtendService.getSignUserName(optionUserId);
        // 准备智能合约订单插入方法所需参数
        List<String> list = new ArrayList<>();
        list.add(this.createMultiplexParam(objList, optionUserId));
        // 调用智能合约
        String string = weBASEUtils.funcPost(userName,contractAddress,functionName, list);
        WeBaseResult weBaseResult = weBASEUtils.convertResult(string);

//   ["xxx,xxx,xxx", "xxx,xxx,xx"]
        return weBaseResult;
    }

    @Override
    public WeBaseResult selectList(Long orderId, Long deptId, Long optionUserId, String functionName) {
        String contractAddress = deptService.getContractAddress(deptId);
        String userName = userExtendService.getSignUserName(optionUserId);
        List<String> list = new ArrayList<>();

        list.add(orderId.toString());

        String string = weBASEUtils.funcPost(userName,contractAddress, functionName, list);
        WeBaseResult result = weBASEUtils.convertViewResult(string);

        return result;

    }

    public WeBaseResult update(Long orderId, Long _id, T obj, Long deptId, Long optionUserId, String functionName){
        // 查询该煎药机构的合约address
        String contractAddress = deptService.getContractAddress(deptId);
        // 查询该操作员的address
        String userName = userExtendService.getSignUserName(optionUserId);
        // 准备智能合约订单插入方法所需参数
        List<String> list = new ArrayList<>();
        String param = this.createParam(obj, optionUserId);
        list.add(String.valueOf(orderId));
        list.add(String.valueOf(_id));
        list.add(param);

        String string = weBASEUtils.funcPost(userName,contractAddress,functionName, list);
        System.out.println(string);
        WeBaseResult weBaseResult = weBASEUtils.convertResult(string);
        System.out.println(weBaseResult);
        return weBaseResult;
    }




    private String createMultiplexParam(List<T> objList, Long optionUserId){
        StringBuffer paramList = new StringBuffer();
        paramList.append("[\"");
        for (int i = 0; i < objList.size(); i++) {
            T obj = objList.get(i);
            String param = this.createParam(obj, optionUserId);
            paramList.append(param).append("\"");
            if(i != objList.size()-1){
                paramList.append(",");
            }
        }
        paramList.append("]");
        System.out.println(paramList);
        return paramList.toString();
    }
}
